﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
using DAL;
using System.Collections.Generic;
using Telerik.Web.UI;

public partial class Timkiem : System.Web.UI.Page

{
    BAIVIETBLL bvBll = new BAIVIETBLL();
    THANHVIENBLL tvBll = new THANHVIENBLL();
    COMMENTBLL cmeBll = new COMMENTBLL();
    PHUMUCBLL pmBll = new PHUMUCBLL();
    CHUYENMUCBLL cmBll = new CHUYENMUCBLL();

    string sql_find = "select bv.mabaiviet,bv.tenbaiviet , bv.noidung,bv.maphumuc ,bv.mathanhvien,bv.ngaylap, bv.matinhtrangbv, bv.listchat, bv.xem " +
                            "from baiviet bv,thanhvien tv,comment cme where bv.mathanhvien = tv.mathanhvien and bv.mabaiviet = cme.mabaiviet";

    protected void Page_Load(object sender, EventArgs e)
    {
        //cach de load 1 lan duy nhat trong 1 trang
        if (Session["tenthanhvien"] == null) Response.Redirect("LogIn.aspx");
        if(!Page.IsPostBack)
        {
            getTreeForum();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (!string.IsNullOrEmpty(TextBox2.Text.Trim()) && string.IsNullOrEmpty(TextBox1.Text.Trim()))
            {
                if (DropDownList3.SelectedIndex == 0)
                {
                    sql_find += " and bv.noidung like '%" + TextBox2.Text + "%'";
                }
                else
                {
                    sql_find += " and bv.tenbaiviet like '%" + TextBox2.Text + "%'";
                }
            }
            else if (string.IsNullOrEmpty(TextBox2.Text.Trim()) && !string.IsNullOrEmpty(TextBox1.Text.Trim()))
            {
                if (CheckBox1.Checked)
                {
                    if (DropDownList4.SelectedIndex == 0)
                    {
                        sql_find += " and cme.mathanhvien = (select tv.mathanhvien from thanhvien tv where tv.tenthanhvien = '" + TextBox1.Text + "')";
                    }
                    else
                    {
                        sql_find += " and bv.mathanhvien = (select tv.mathanhvien from thanhvien tv where tv.tenthanhvien = '" + TextBox1.Text + "')";
                    }
                }
                else
                {
                    if (DropDownList4.SelectedIndex == 0)
                    {
                        sql_find += " and cme.mathanhvien = (select tv.mathanhvien from thanhvien tv where tv.tenthanhvien like '%" + TextBox1.Text + "%')";
                    }
                    else
                    {
                        sql_find += " and bv.mathanhvien = (select tv.mathanhvien from thanhvien tv where tv.tenthanhvien like '%" + TextBox1.Text + "%')";
                    }
                }

            }
            //2 cái deu có
            else if (!string.IsNullOrEmpty(TextBox2.Text.Trim()) && !string.IsNullOrEmpty(TextBox1.Text.Trim()))
            {
                if (DropDownList3.SelectedIndex == 0)
                {
                    sql_find += " and bv.noidung like '%" + TextBox2.Text + "%'";
                }
                else
                {
                    sql_find += " and bv.tenbaiviet like '%" + TextBox2.Text + "%'";
                }

                if (CheckBox1.Checked)
                {
                    if (DropDownList4.SelectedIndex == 0)
                    {
                        sql_find += " and cme.mathanhvien = (select tv.mathanhvien from thanhvien tv where tv.tenthanhvien = '" + TextBox1.Text + "')";
                    }
                    else
                    {
                        sql_find += " and bv.mathanhvien = (select tv.mathanhvien from thanhvien tv where tv.tenthanhvien = '" + TextBox1.Text + "')";
                    }
                }
                else
                {
                    if (DropDownList4.SelectedIndex == 0)
                    {
                        sql_find += " and cme.mathanhvien = (select tv.mathanhvien from thanhvien tv where tv.tenthanhvien like '%" + TextBox1.Text + "%')";
                    }
                    else
                    {
                        sql_find += " and bv.mathanhvien = (select tv.mathanhvien from thanhvien tv where tv.tenthanhvien like '%" + TextBox1.Text + "%')";
                    }
                }

            }
        }
        catch
        {

        }
        finally
        {
            showList(sql_find);
        }
    }
   
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            this.Button1_Click(sender, e);
            //tim theo chu de
            if (!string.IsNullOrEmpty(txttraloi.Text))
            {
                if (droptimchude.SelectedIndex == 0)
                {
                    sql_find += " and (select count(*)from comment cme where cme.mabaiviet = bv.mabaiviet)>= " + Int32.Parse(txttraloi.Text);
                }
                else
                {
                    sql_find += " and (select count(*)from comment cme where cme.mabaiviet = bv.mabaiviet)<= " + Int32.Parse(txttraloi.Text);
                }
            }
            //tim theo thoi gian
            if (dropmoihon.SelectedIndex == 0)
            {
                int ngay = 0;
                sql_find += " and bv.ngaylap > (select getdate() - ";
                switch (dropthoigian.SelectedIndex)
                {
                    case 0:
                        ngay = 0;
                        break;
                    case 1:
                        ngay = 1;
                        break;
                    case 2:
                        ngay = 7;
                        break;
                    case 3:
                        ngay = 14;
                        break;
                    case 4:
                        ngay = 30;
                        break;
                    case 5:
                        ngay = 90;
                        break;
                    case 6:
                        ngay = 182;
                        break;
                    case 7:
                        ngay = 365;
                        break;
                }

                sql_find += ngay.ToString() + ") ";
            }
            else
            {
                int ngay = 0;
                sql_find += " and bv.ngaylap < (select getdate() - ";
                switch (dropthoigian.SelectedIndex)
                {
                    case 0:
                        ngay = 0;
                        break;
                    case 1:
                        ngay = 1;
                        break;
                    case 2:
                        ngay = 7;
                        break;
                    case 3:
                        ngay = 14;
                        break;
                    case 4:
                        ngay = 30;
                        break;
                    case 5:
                        ngay = 90;
                        break;
                    case 6:
                        ngay = 182;
                        break;
                    case 7:
                        ngay = 365;
                        break;
                }

                sql_find += ngay.ToString() + ") ";
            }
            //get an example
            //get value form treeview for sql string
            /*string str_tree_forum = rad_tree_forum.SelectedNode.Text;
            if (!str_tree_forum.Equals("Tất cả"))
                sql_find += " and (pm.tenphumuc = ' " + str_tree_forum + " ' or cm.tenchuyenmuc = ' "
                + str_tree_forum + " ') ";
            */
            //sap xep tang giam
            string sql = " select * from ( ";
            if (droptangdan.SelectedIndex == 0)
            {
                switch (dropsapxep.SelectedIndex)
                {
                    case 0:
                        sql_find += " order by bv.ngaylap ASC";
                        break;
                    case 1:
                        sql_find += " order by bv.tenbaiviet ASC";
                        break;
                    case 2:
                        sql += sql_find;
                        sql += " )a ";
                        sql += " order by (select count(*)from comment cme where cme.mabaiviet = mabaiviet) ASC";
                        break;
                }
            }
            else
            {
                switch (dropsapxep.SelectedIndex)
                {
                    case 0:
                        sql_find += " order by bv.ngaylap DESC";
                        break;
                    case 1:
                        sql_find += " order by bv.tenbaiviet DESC";
                        break;
                    case 2:
                        sql += sql_find;
                        sql += " )a ";
                        sql += " order by (select count(*)from comment cme where cme.mabaiviet = mabaiviet) DESC";
                        break;
                }
            }
        }
        catch (Exception h) {
            string t = h.Message;
            Response.Redirect("Timkiem.aspx");
        }
    }
    public void getTreeForum()
    {
        RadTreeNode nodeRoot = new RadTreeNode();
        nodeRoot.Text = "Tất cả";

        List<CHUYENMUC> listCm = cmBll.getALL();

        foreach (CHUYENMUC cm in listCm)
        {
            RadTreeNode newNode = new RadTreeNode();
            newNode.Text = cm.tenchuyenmuc;


            List<PHUMUC> listPm = pmBll.getPhumucWithIDCm(cm.machuyenmuc);
            foreach (PHUMUC pm in listPm)
            {
                RadTreeNode newNodePm = new RadTreeNode();
                newNodePm.Text = pm.tenphumuc;

                newNode.Nodes.Add(newNodePm);
            }

            nodeRoot.Nodes.Add(newNode);
        }
          //showList(sql_find);
    }
    public void showList(string sql)
    {
        List<BAIVIET> listBv = bvBll.getListBaivietWithSQLString(sql);
        string str = "";

        str += "<div class=\"aboutus_bg\">"
                    + "<table>"
                    + "<tr>"
                    + "<td class=\"about_col0\"><div class=\"about_div_default\">TT</div></td>"
                    + "<td class=\"about_col1\"><div class=\"about_div_default\">Phụ mục</div></td>"
                    + "<td class=\"about_col2\"><div class=\"about_div_default\">Người gửi</div></td>"
                    + "<td class=\"about_col3\"><div class=\"about_div_default\">Xem</div></td>"
                    + "<td class=\"about_col4\"><div class=\"about_div_default\">Trả lời</div></td>"
                    + "</tr>";

        str += "<tr>"
            + "<td>Chủ đề khác</td>"
            + "</tr>";
        str += "<tr>"
                    + "<td>Các bài viết đã tìm được</td>"
                    + "</tr>";

        foreach (BAIVIET bv in listBv)
        {
           
            THANHVIEN tv = tvBll.getThanhvienWithIDBaiviet(bv.mabaiviet);
            int count_comment = cmeBll.getCountOfCommentByIDBaiviet(bv.mabaiviet);

            if (bvBll.getTinhtrangWithID(bv.mabaiviet) == "UnBlock")
            {
                str += "<tr>"
                            + "<td class=\"about_col0\"><div class=\"normal_div_boder\"></div></td>"
                            + "<td class=\"about_col1\"><div class=\"normal_div_boder\"><p>"
                            + "<a href=\"Baiviet.aspx?mabv=" + bv.mabaiviet + "&tenbv=" + bv.tenbaiviet + "\"runat=\"server\">" + bv.tenbaiviet + "</a>"
                            + "</p></div></td>"
                            + "<td class=\"about_col2\"><div class=\"normal_div_boder\">" + tv.tenthanhvien + "<br/>" + bv.ngaylap.ToShortDateString() + " - " + bv.ngaylap.ToShortTimeString() + "</div></td>"
                            + "<td class=\"about_col3\"><div class=\"normal_div_boder\">" + bv.xem + "</div></td>"
                            + "<td class=\"about_col4\"><div class=\"normal_div_boder\">" + count_comment + "</div></td>"
                            + "</tr>";
            }
            else {
                str += "<tr>"
                            + "<td class=\"about_col0_block\"><div class=\"normal_div_boder\"></div></td>"
                            + "<td class=\"about_col1_block\"><div class=\"normal_div_boder\"><p>"
                            + "<a href=\"Baiviet.aspx?mabv=" + bv.mabaiviet + "&tenbv=" + bv.tenbaiviet + "\"runat=\"server\">" + bv.tenbaiviet + "</a>"
                            + "</p></div></td>"
                            + "<td class=\"about_col2_block\"><div class=\"normal_div_boder\">" + tv.tenthanhvien + "<br/>" + bv.ngaylap.ToShortDateString() + " - " + bv.ngaylap.ToShortTimeString() + "</div></td>"
                            + "<td class=\"about_col3_block\"><div class=\"normal_div_boder\">" + bv.xem + "</div></td>"
                            + "<td class=\"about_col4_block\"><div class=\"normal_div_boder\">" + count_comment + "</div></td>"
                            + "</tr>";
            }
        }

        str += "</table>"
                    + "</div>";


        p_search.InnerHtml = str;
    }
}
